<template>
	<view>
		<!-- 按照要求实现微信小程序授权 -->
		<image :src="avatarUrl"></image>
		<view class="username">您好，{{ nickName }}</view>
		<button type="primary" @click="login">微信授权登录</button>
	</view>
	
	
</template>

<script>
	import useUserStore from '@/store/user'
	export default {
	  data() {
	    return {
			avatarUrl:"",
			nickName:""
	    }
	  },
	  methods: {	
	    login() {
	      if (uni.getUserProfile) {
	        //获取用户信息
	        uni.getUserProfile({
	          lang: 'zh_CN',
	          desc: '用来授权登录该小程序！',
	          success: (res) => {
	            //https://uniapp.dcloud.net.cn/tutorial/app-oauth-weixin.html
	            uni.login({
	              "provider": "weixin",
	
	              "onlyAuthorize": true, 
	              success: function(event){
	                //获取code
	                const {code} = event
	                const wxInfo = {
	                  appid:'wx9fcceb65d10c992b', //请填入自己的appid
	                  secret:'4996e5f12b1b4956a048e22df88e7819', //请填入自己获取的密钥
	                  js_code:code,
	                  grant_type:'authorization_code'
	                };
	                //获取openid
	                uni.request({
	                  url: 'https://api.weixin.qq.com/sns/jscode2session', 
	                  method:'GET',
	                  data: wxInfo,
	                  success: (result) => {
	                    const userStore = useUserStore()
	                    const { data } = result;
	
	                    //保存信息
	                    userStore.setToken(data.openid);
	                    userStore.fillUser(res.userInfo);
						
						const { nickName, avatarUrl1 } = userStore.userinfo;
						this.nickName = nickName;
						this.avatarUrl = avatarUrl1;
	                    uni.showToast({title:'登录成功'});
	                    // uni.switchTab({url:'/pages/my/my'})
	                  }
	                });
	              },
	              fail: function (err) {
	                uni.showToast({title:'登录失败'});
	              }
	            })	
	
	          }
	        })
	      }
	    }
	  }
	}
</script>

<style>

</style>